package com.shhm.web.controller;

import com.github.benmanes.caffeine.cache.Cache;
import com.shhm.common.context.PermsContextHolder;
import com.shhm.common.dto.response.AjaxResult;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.entity.admin.SysRoleOperatePerms;
import com.shhm.common.utils.IpUtil;
import com.shhm.common.utils.JwtUtil;
import com.shhm.common.utils.LocalCacheUtil;
import com.shhm.common.utils.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author JackZ
 * @version 1.0
 * @description: 测试接口
 * @date 2025/7/13 下午3:00
 */
@RestController
//@RequestMapping("/test")
public class TestApiController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Resource(name = "PermissionsCache")
    private Cache<String, List<SysRoleOperatePerms>> localPermissionsCache;

    @GetMapping("/list")
    public String test(HttpServletRequest request) {
        String ipAddress = IpUtil.getIpAddress(request);
        System.out.println("Key Serializer: " + redisTemplate.getKeySerializer().getClass().getName());
        System.out.println("Hash Key Serializer: " + redisTemplate.getHashKeySerializer().getClass().getName());
        System.out.println("Value Serializer: " + redisTemplate.getValueSerializer().getClass().getName());
        System.out.println("Hash Value Serializer: " + redisTemplate.getHashValueSerializer().getClass().getName());

        Boolean ipExists = redisTemplate.opsForHash().hasKey(RedisUtil.REDIS_LOGIN_KEY+ipAddress, ipAddress);
        System.out.println(ipExists+"-");
        return "Check console";
    }

    @GetMapping("/csjwt")
    public AjaxResult cstoken(HttpServletRequest request){
        String auth = request.getHeader("Authorization");
        String roleFromToken = JwtUtil.getRoleFromToken(auth).toString();

        return AjaxResult.success(roleFromToken);
    }

    @GetMapping("/admin/idx")
    public AjaxResult csadmin(){
        return AjaxResult.success("admin");
    }

    @GetMapping("/user/idx")
    public AjaxResult csuser(){
        return AjaxResult.success("user");
    }

    @GetMapping("/test/cache/{roleKey}")
    public Object getFromCache(@PathVariable String roleKey) {
        String key = RedisUtil.REDIS_OPERATE_PERMS_KEY + roleKey;
        List<SysRoleOperatePerms> perms = localPermissionsCache.getIfPresent(key);
        Object o = perms != null ? perms : "缓存中无此角色权限";
        return AjaxResultPlus.success(o,false);
    }

    @GetMapping("/admin/context/perms")
    public AjaxResult flushCache(){
//        SysRoleOperatePerms permissions = PermsContextHolder.getPermissions();
        return AjaxResult.success(SecurityContextHolder.getContext().getAuthentication());
    }

    @GetMapping("/test/cache/clear")
    public AjaxResult clearLocalCache(){
//        localPermissionsCache.invalidateAll();
        LocalCacheUtil.clearAllLocalCache(localPermissionsCache);
        return AjaxResult.success("执行成功！");
    }

    @GetMapping("/test/get/cache/local/{role}")
    public AjaxResult getLocalCache(String role){
        return AjaxResult.success(localPermissionsCache.getIfPresent(LocalCacheUtil.CACHE_PERMS_KEY_PREFIX + role));
    }

    @GetMapping("/test/security/user")
    public AjaxResult getAuthSecurityUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return AjaxResult.success(authentication);
    }
}
